package com.zlx.xBiaoShop.service;

import com.zlx.xBiaoShop.dao.RecvAddrDao;
import com.zlx.xBiaoShop.dao.UserDao;
import com.zlx.xBiaoShop.entity.RecvAddr;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
public class RecvAddrService {
    @Autowired
    private RecvAddrDao recvAddrDao;


    /*
    查询所有的收货地址
     */
    public List<RecvAddr> findAllAddrByUserId(Integer userId)
    {
        return recvAddrDao.findByUserId(userId);
    }
    /*
        根据id删除收货地址
     */
    public void deleteById(Integer id) {
        recvAddrDao.deleteById(id);
    }
    /*
     新添加地址
     */
    @Transactional
    public Integer save(RecvAddr addr, Boolean flag, Integer userId) {
        addr.setUserId(userId);
        addr.setPostCode("000000");
        RecvAddr save = recvAddrDao.save(addr);
        return save.getId();
    }
    public String getDefaultAddr(Integer userId, Integer defaultId) {
        RecvAddr recvAddr = recvAddrDao.findByUserIdAndId(userId, defaultId);
        return recvAddr.getArea()+recvAddr.getDetailAddr();
    }
}
